package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ClueAutoAllocationDetailDTO;
import com.xbongbong.paas.pojo.dto.ClueAutoAllocationListSortDTO;
import com.xbongbong.paas.pojo.dto.ClueAutoAllocationSaveDTO;
import com.xbongbong.paas.pojo.vo.ClueAutoAllocationDetailVO;
import com.xbongbong.paas.pojo.vo.ClueAutoAllocationListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.AutomaticallyAssignedService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ClueErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.PublicSeniorRuleHelp;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2021/1/15 11:02
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/automaticallyAssigned")
public class AutomaticallyAssignedController {
    
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private AutomaticallyAssignedService automaticallyAssignedService;
    @Resource
    private PublicSeniorRuleHelp publicSeniorRuleHelp;

    /**
     * 【web】规则列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<ClueAutoAllocationListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ClueAutoAllocationListVO clueAutoAllocationListVO = automaticallyAssignedService.distributionList(baseDTO);
                response = new XbbResponse<>(clueAutoAllocationListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/distribution/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String distributionSort(@RequestBody @Valid ClueAutoAllocationListSortDTO clueAutoAllocationListSortDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(clueAutoAllocationListSortDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(clueAutoAllocationListSortDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(clueAutoAllocationListSortDTO.getCorpid(), XbbRefTypeEnum.CLUE.getCode())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO baseVO = automaticallyAssignedService.distributionSort(clueAutoAllocationListSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/distribution/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String distributionDetail(@RequestBody @Valid ClueAutoAllocationDetailDTO clueAutoAllocationDetailDTO, BindingResult br) {
        XbbResponse<ClueAutoAllocationDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ClueAutoAllocationDetailVO clueAutoAllocationDetailVO = automaticallyAssignedService.distributionDetail(clueAutoAllocationDetailDTO);
                response = new XbbResponse<>(clueAutoAllocationDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/distribution/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String distributionSave(@RequestBody @Valid ClueAutoAllocationSaveDTO clueAutoAllocationSaveDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(clueAutoAllocationSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(clueAutoAllocationSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(clueAutoAllocationSaveDTO.getCorpid(), XbbRefTypeEnum.CLUE.getCode())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO baseVO = automaticallyAssignedService.distributionSave(clueAutoAllocationSaveDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/distribution/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String distributionStatusUpdate(@RequestBody @Valid ClueAutoAllocationDetailDTO clueAutoAllocationDetailDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(clueAutoAllocationDetailDTO.getCorpid(), XbbRefTypeEnum.CLUE.getCode())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                BaseVO baseVO = automaticallyAssignedService.distributionDel(clueAutoAllocationDetailDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
